package functions.ems_claim;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;
import com.beans.EmpDeptOrProjectCostBean;
import com.crc.mam.BaseCase;
import coms.Constants;
import coms.JSONToBean;
import frame.HttpHelper;


public class AccountDeptCost extends BaseCase {
  private org.slf4j.Logger logger = LoggerFactory.getLogger(AccountDeptCost.class);
  
  public EmpDeptOrProjectCostBean queryEmpDeptCostByApplOrgDept(String deptId) {
    logger.info("queryEmpDeptCostByApplOrgDept   ---- - deptId :" + deptId);
    EmpDeptOrProjectCostBean bean = null;
    String params = "{\"applOrgDept\":\""+deptId+"\"}";
    String url = Constants.QUERY_EMP_DEPT_COST_BY_APPL_ORG_DEPT_URL +"/"+deptId;
    try {
      result = HttpHelper.post(params, url);
      status = HttpHelper.isOk;
      logger.info("queryEmpDeptCostByApplOrgDept" + result);
      if(status) {
        bean = JSONToBean.parseJson2Object(EmpDeptOrProjectCostBean.class, result.toString());
        remark = bean.toString();
        logger.info(remark);
      }else {
        status = false;
        try {
          JSONObject object = new JSONObject(result);
          remark = object.getString("message");
        }catch(JSONException e) {
          e.printStackTrace();
          remark  = "JSON解析错误";
        } 
      }
    }catch(Exception e) {
      e.printStackTrace();
      remark = "系统异常";
      status = false;
    }
    logger.info("queryEmpDeptCostByApplOrgDept   ---- - end");
    return bean;
  }
  
  public EmpDeptOrProjectCostBean queryCostDeptByProjectId(String projectCode) {
    logger.info("queryCostDeptByProjectId   ---- - projectCode: "+projectCode);
    EmpDeptOrProjectCostBean costBean = null;
    String url  = Constants.QUERY_COST_DEPT_BY_PROJECT_ID_URL+"/"+projectCode;
    try {
      result = HttpHelper.post("", url);
      status = HttpHelper.isOk;
      logger.info("queryCostDeptByProjectId result -->" +result);
      if(status) {
        try {
          JSONArray array = new JSONArray(result);
          JSONObject jsonObject = array.getJSONObject(0);
          costBean = JSONToBean.parseJson2Object(EmpDeptOrProjectCostBean.class, jsonObject.toString());
          logger.info(costBean.toString());
          remark = costBean.toString();
        }catch(JSONException e) {
          e.printStackTrace();
          remark = "JSON解析异常";
        }
      }else {
        try {
          JSONObject object = new JSONObject(result);
          remark = object.getString("message");
        }catch(JSONException e) {
          e.printStackTrace();
          remark = "JSON解析异常";
        }
      }
    }catch(Exception e) {
      status = false;
      e.printStackTrace();
      remark = "系统异常";
    }
    
    return costBean;
  }
}
